<template>
  <a-modal
      width="50%"
      height="90%"
      :maskClosable="true"
      :visible="props.visible"
      :confirm-loading="loading"
      :forceRender="true"
      title="新增患者"
      :body-style="{ paddingBottom: '8px', height: '520px', overflowY: 'auto' }"
      @update:visible="updateVisible"
      @ok="save"
      @close="updateVisible(false)"
  >
      <div>
        <a-radio-group v-model:value="mode" :style="{ marginBottom: '8px' }">
          <a-radio-button value="his">his拉取</a-radio-button>
          <a-radio-button value="self">手动新增</a-radio-button>
        </a-radio-group>
      </div>
        <a-form :label-col="{ sm: { span: 6 } }" :wrapper-col="{ sm: { span: 18 } }">
          <a-form-item label="病案号" v-bind="validateInfos.inNo">
            <a-input v-model:value="form.inNo" placeholder="请输入病案号" />
          </a-form-item>
          <a-form-item  label="病种" v-bind="validateInfos.diseaseCode">
            <a-select
                v-model:value="form.diseaseCode"
                placeholder="请选择病种" allow-clear
            >
              <a-select-option
                  v-for="dict in diseaseList"
                  :key="dict.dictCode"
                  :value="dict.dictCode"
              >
                {{ dict.dictName }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="姓名" v-if="mode == 'self'" v-bind="validateInfos.name">
            <a-input v-model:value="form.name" placeholder="请输入姓名" />
          </a-form-item>
          <a-form-item  label="性别"  v-if="mode == 'self'"  v-bind="validateInfos.sex">
            <a-select
                v-model:value="form.sex"
                placeholder="请选择性别" allow-clear

            >
              <a-select-option key="M" value="M"> 男 </a-select-option>
              <a-select-option key="F" value="F"> 女 </a-select-option>
            </a-select>
          </a-form-item>

    <a-form-item v-if="mode == 'self'"  label="入院日期"  v-bind="validateInfos.inDate">
      <a-date-picker  v-model:value="form.inDate"
                      format="YYYY-MM-DD HH:mm"
                      valueFormat="YYYY-MM-DD HH:mm"
                      show-time
                      style="width: 100%;"
      />
    </a-form-item>
    <a-form-item v-if="mode == 'self'"  label="出院日期"  v-bind="validateInfos.outDate">
      <a-date-picker  v-model:value="form.outDate"
                      format="YYYY-MM-DD HH:mm"
                      valueFormat="YYYY-MM-DD HH:mm"
                      show-time
                      style="width: 100%;"
      />
    </a-form-item>
        </a-form>
  </a-modal>
</template>
<script setup name="PatientAdd">
import {ref, reactive, onBeforeMount} from 'vue';
import { PatientApi } from '../api/PatientApi';
import {Form, message} from "ant-design-vue";
const useForm = Form.useForm;
const props = defineProps({
  visible: Boolean
});
const emits = defineEmits(['update:visible', 'done']);
const mode = ref('his');
const activeKey = ref(1);
// 弹框加载
const loading = ref(false);
const updateVisible = value => {
  emits('update:visible', value);
};
const formRef = ref(null);
const form = reactive({
  inNo: '',
  diseaseCode: '',
  name: '',
  sex: '',
  inDate: '',
  outDate: '',
  diseaseName:''
});
// 表单验证规则
const rules = reactive({
  inNo: [
    {
      required: true,
      type: 'string',
      message: '请输入病案号',
      trigger: 'blur'
    }
  ],
  diseaseCode: [
    {
      required: true,
      type: 'string',
      message: '请选择病种'
    }
  ],
  name: [
    {
      required: true,
      type: 'string',
      message: '请输入姓名',
      trigger: 'blur'
    }
  ],
  sex: [
    {
      required: true,
      type: 'string',
      message: '请选择性别'
    }
  ]
});
const { resetFields, validate, validateInfos } = useForm(form, rules);
const diseaseList = ref(null);
const getDictData = () => {
  PatientApi.findDictList({ dictTypeId: '1813510904349069313' }).then(res => {
    diseaseList.value = res.data;
  });
};
onBeforeMount(() => {
  getDictData();
});
const save = async () => {
  validate()
      .then(() => {
        loading.value = true;
        form.type = mode.value;
        for(let i = 0; i < diseaseList.value.length; i ++) {
          if (form.diseaseCode ==  diseaseList.value[i].dictCode) {
            form["diseaseName"]=diseaseList.value[i].dictName
          }
        }
        PatientApi.save(form)
            .then(msg => {
              loading.value = false;
              updateVisible(false);
              // 触发父组件done事件
              emits('done');
            })
            .catch(e => {
              loading.value = false;
            });
      })
      .catch(() => {});
};
</script>
<style scoped lang="less">
</style>